<?php
/**
 * Created by PhpStorm.
 * User: coder
 * Date: 2018/11/24
 * Time: 12:27
 */

namespace util;

/**
 * Class Returnutil
 * @desc   统一接口返回类
 * @package util
 */
class Returnutil
{
    const SERVER_SUCCESS_CODE = 200;
    const SERVER_FAILED_CODE = 101; //服务器接受了请求,但请求失败了,此时程序正常进行,详情请查看message
    const SERVER_ERROR_CODE = 500;

    const SERVER_SUCCESS_MESSAGE = "成功";
    const SERVER_FAILED_MESSAGE = "失败";
    const SERVER_ERROR_MESSAGE = "异常";

    private $code = self::SERVER_SUCCESS_CODE;
    private $msg;
    private $data;

    private static $instance = null;

    //禁止类外实例化
    private function __construct()
    {

    }

    public static function getInstance()
    {
        return self::$instance = new self();
    }

    public function setCode($code)
    {
        $this->code = $code;
        return $this;
    }

    public function getCode()
    {
        return $this->code;
    }

    public function setMessage($msg)
    {
        $this->msg = $msg;
        return $this;
    }

    public function getMessage()
    {
        return $this->msg;
    }

    public function setData($data)
    {
        $this->data = $data;
        return $this;
    }

    public function getData()
    {
        return $this->data;
    }

    public function asArray()
    {
        return [
            'code' => $this->code,
            'msg' => $this->msg,
            'data' => $this->data,
        ];
    }

    public function asJson()
    {
        return json($this->asArray());
    }
}